<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Send database signal</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_example_c_gensig.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Send database signal </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>samples\example\c\gensig.c</b> <br />
This example uses the CAN Database gensig.dbc <br />
</p><div class="fragment"><div class="line"><span class="comment">/* gensig.c: Signal generator for CANdb testing */</span></div><div class="line"></div><div class="line"><span class="comment">/***************************************************************************/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;windows.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;mmsystem.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;math.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;time.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="comment">/***************************************************************************/</span></div><div class="line"><span class="comment">/* Defines: */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#ifndef M_PI</span></div><div class="line"><span class="preprocessor">#  define M_PI          3.14159265358979323846</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="comment">/***************************************************************************/</span></div><div class="line"><span class="comment">/* Static variables: */</span></div><div class="line"></div><div class="line"><span class="comment">/***************************************************************************/</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> errorExit (<span class="keywordtype">char</span> *message, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    <span class="keywordtype">char</span> buf [50];</div><div class="line">    buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    <a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a> (stat, buf, <span class="keyword">sizeof</span> (buf));</div><div class="line">    fprintf (stderr, <span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, message, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">    exit (1);</div><div class="line">  }</div><div class="line">} <span class="comment">/* errorExit */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> usage (<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  printf (<span class="stringliteral">&quot;\nGenSig - a sample program from CANLIB SDK (http://www.kvaser.com)\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;This program generates a stream of CAN messages. The data in the messages\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;form a signal whose shape can be selected using the command-line flags\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;described below.\n\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;You can open gensig.dbc with the Kvaser Database Editor to look\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;at the signal(s).\n\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -B&lt;speed&gt;: Set the CAN bus bit rate; 1000, 500, 250 or 125 kbps.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -rt: Boost our priority to realtime.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -all: Generate all shapes.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -trig: Generate a sine and a cosine wave.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -ramp: Generate a ramp.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -digital: Generate a square wave.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -float: Generate messages with floating point numbers,\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;          forming sine/cosine waves.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -motorola: Use Motorola byte order (Big Endian.)\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  -sleep&lt;N&gt;: Sleep N ms extra in the main loop (use to slow down program).\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;The program prints the following characters:\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  *  One character per 1000 &#39;trig&#39; messages.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  #  One character per 1000 &#39;digital&#39; messages.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  +  One character per 1000 &#39;ramp&#39; messages.\n&quot;</span></div><div class="line">          <span class="stringliteral">&quot;  .  One character per 1000 &#39;float&#39; messages.\n&quot;</span></div><div class="line">         );</div><div class="line"></div><div class="line">  exit (1);</div><div class="line">} <span class="comment">/* usage */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help (<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  printf (<span class="stringliteral">&quot;Available commands:\n&quot;</span>);</div><div class="line">  printf (<span class="stringliteral">&quot;  q: Change debug level\n&quot;</span>);</div><div class="line">  printf (<span class="stringliteral">&quot;ESC: Quit.\n&quot;</span>);</div><div class="line">} <span class="comment">/* help */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">** Use the performance counter to get the current time.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> get_time_1ms (<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  LARGE_INTEGER freq, time;</div><div class="line">  QueryPerformanceFrequency (&amp;freq);</div><div class="line">  QueryPerformanceCounter (&amp;time);</div><div class="line">  freq.QuadPart /= 1000;</div><div class="line">  time.QuadPart /= freq.QuadPart;</div><div class="line">  <span class="keywordflow">return</span> time.LowPart;</div><div class="line">} <span class="comment">/* get_time_1ms */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> clear_msg (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *msg)</div><div class="line">{</div><div class="line">  memset (msg, 0, 8);</div><div class="line">} <span class="comment">/* clear_msg */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> store_int (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *msg, <span class="keywordtype">int</span> pos, <span class="keywordtype">int</span> len, <span class="keywordtype">int</span> value, <span class="keywordtype">int</span> motorola)</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> bpos = 0, bit = 0;</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> ((pos &gt;= 64) || (pos &lt; 0) || (len &lt; 1) || ((pos + len - 1) &gt;= 64)) <span class="keywordflow">return</span> -1;</div><div class="line">  <span class="keywordflow">if</span> (!msg) <span class="keywordflow">return</span> -1;</div><div class="line"></div><div class="line">  bpos = pos / 8;</div><div class="line">  bit = pos % 8;</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (len &gt; 0) {</div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> mask = 0, v;</div><div class="line">    <span class="keywordflow">if</span> (len == 1) mask = 0x01;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 2) mask = 0x03;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 3) mask = 0x07;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 4) mask = 0x0f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 5) mask = 0x1f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 6) mask = 0x3f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 7) mask = 0x7f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len &gt;= 8) mask = 0xff;</div><div class="line">    mask = (mask &lt;&lt; bit) &amp; 0xff;</div><div class="line">    <span class="keywordflow">if</span> (motorola) {</div><div class="line">      <span class="keywordtype">int</span> sh = len - (8 - bit);</div><div class="line">      <span class="keywordflow">if</span> (sh &lt; 0) sh = 0;</div><div class="line">      v = ((value &gt;&gt; sh) &lt;&lt; bit) &amp; 0xff;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> v = (value &lt;&lt; bit) &amp; 0xff;</div><div class="line">    msg [bpos] = (msg [bpos] &amp; ~mask) | (v &amp; mask);</div><div class="line">    <span class="keywordflow">if</span> (!motorola) value = value &gt;&gt; (8 - bit);</div><div class="line">    len -= 8 - bit;</div><div class="line">    bit = 0;</div><div class="line">    ++bpos;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">} <span class="comment">/* store_int */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> store_float (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *msg, <span class="keywordtype">int</span> pos, <span class="keywordtype">float</span> f_value, <span class="keywordtype">int</span> motorola)</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> bpos = 0, bit = 0;</div><div class="line">  <span class="keywordtype">int</span> len = <span class="keyword">sizeof</span> (float) * 8;</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> value = 0;</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (<span class="keyword">sizeof</span> (value) != <span class="keyword">sizeof</span> (f_value)) <span class="keywordflow">return</span> -1;</div><div class="line"></div><div class="line">  memcpy (&amp;value, &amp;f_value, <span class="keyword">sizeof</span> (value));</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> ((pos &gt;= 64) || (pos &lt; 0) || (len &lt; 1) || ((pos + len - 1) &gt;= 64)) <span class="keywordflow">return</span> -1;</div><div class="line">  <span class="keywordflow">if</span> (!msg) <span class="keywordflow">return</span> -1;</div><div class="line"></div><div class="line">  bpos = pos / 8;</div><div class="line">  bit = pos % 8;</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (len &gt; 0) {</div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> mask = 0, v;</div><div class="line">    <span class="keywordflow">if</span> (len == 1) mask = 0x01;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 2) mask = 0x03;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 3) mask = 0x07;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 4) mask = 0x0f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 5) mask = 0x1f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 6) mask = 0x3f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len == 7) mask = 0x7f;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (len &gt;= 8) mask = 0xff;</div><div class="line">    mask = (mask &lt;&lt; bit) &amp; 0xff;</div><div class="line">    <span class="keywordflow">if</span> (motorola) {</div><div class="line">      <span class="keywordtype">int</span> sh = len - (8 - bit);</div><div class="line">      <span class="keywordflow">if</span> (sh &lt; 0) sh = 0;</div><div class="line">      v = ((value &gt;&gt; sh) &lt;&lt; bit) &amp; 0xff;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> v = (value &lt;&lt; bit) &amp; 0xff;</div><div class="line">    msg [bpos] = (msg [bpos] &amp; ~mask) | (v &amp; mask);</div><div class="line">    <span class="keywordflow">if</span> (!motorola) value = value &gt;&gt; (8 - bit);</div><div class="line">    len -= 8 - bit;</div><div class="line">    bit = 0;</div><div class="line">    ++bpos;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">} <span class="comment">/* store_float */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">/***************************************************************************/</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">  <a class="code" href="kvmlib_8h.html#aa8c0374618b33785ccb02f74bcfebc46">HANDLE</a> notification_event;</div><div class="line">  <span class="keywordtype">int</span> i;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">  <span class="keywordtype">int</span> ready = 0;</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> btr0 = 0, btr1 = 0;</div><div class="line"></div><div class="line">  <span class="keywordtype">int</span> send_trig = 0,</div><div class="line">      send_digital = 0,</div><div class="line">      send_ramp = 0,</div><div class="line">      send_float = 0,</div><div class="line">      rt = 0,</div><div class="line">      motorola = 0;</div><div class="line"></div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> trig_id = 100,</div><div class="line">               digital_id = 101,</div><div class="line">               ramp_id = 102,</div><div class="line">               float_id = 103;</div><div class="line"></div><div class="line">  <span class="keywordtype">double</span> sin1_period = 1.0,</div><div class="line">         sin2_period = 0.7,</div><div class="line">         sin1_amp = 100,</div><div class="line">         sin2_amp = 100,</div><div class="line">         cos1_period = 1.5,</div><div class="line">         cos2_period = 0.5,</div><div class="line">         cos1_amp = 100,</div><div class="line">         cos2_amp =  50;</div><div class="line"></div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> tstart;</div><div class="line"></div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> iterations = 0;</div><div class="line"></div><div class="line">  <span class="keywordtype">int</span> tseg1 = 0,</div><div class="line">      tseg2 = 0,</div><div class="line">      sjw = 0,</div><div class="line">      nosamp = 0,</div><div class="line">      sync_mode = 0,</div><div class="line">      prescaler = 0;</div><div class="line">  <span class="keywordtype">long</span> freq;</div><div class="line">  <span class="keywordtype">int</span> baudrate = <a class="code" href="canlib_8h.html#ac823cae7d94763607a7ca40fcdb5196d">canBITRATE_1M</a>,</div><div class="line">      baudrate_raw = -1,</div><div class="line">      channel = 0;</div><div class="line">  <span class="keywordtype">int</span> debug_level = 0;</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> seq = 0;</div><div class="line">  <span class="keywordtype">unsigned</span> extra_sleep = 0;</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a> ();</div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (i = 1; i &lt; argc; i++) {</div><div class="line">    <span class="keywordtype">int</span> tmp;</div><div class="line">    <span class="keywordtype">char</span> c;</div><div class="line">    <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      channel = tmp;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-sleep%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      extra_sleep = tmp;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-sleep&quot;</span>) == 0) {</div><div class="line">      ++i;</div><div class="line">      <span class="keywordflow">if</span> (i &gt;= argc) usage ();</div><div class="line">      <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) extra_sleep = tmp;</div><div class="line">      <span class="keywordflow">else</span> usage ();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-B%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      baudrate_raw = tmp;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-B&quot;</span>) == 0) {</div><div class="line">      ++i;</div><div class="line">      <span class="keywordflow">if</span> (i &gt;= argc) usage ();</div><div class="line">      <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) baudrate_raw = tmp;</div><div class="line">      <span class="keywordflow">else</span> usage ();</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-rt&quot;</span>) == 0) {</div><div class="line">      rt = 1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-all&quot;</span>) == 0) {</div><div class="line">      send_trig = 1;</div><div class="line">      send_digital = 1;</div><div class="line">      send_ramp = 1;</div><div class="line">      send_float = 1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-trig&quot;</span>) == 0) {</div><div class="line">      send_trig = 1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-digital&quot;</span>) == 0) {</div><div class="line">      send_digital = 1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-ramp&quot;</span>) == 0) {</div><div class="line">      send_ramp = 1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-float&quot;</span>) == 0) {</div><div class="line">      send_float = 1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp (argv [i], <span class="stringliteral">&quot;-motorola&quot;</span>) == 0) {</div><div class="line">      motorola = 1;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> {</div><div class="line">      usage ();</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (baudrate_raw != -1) {</div><div class="line">    <span class="keywordflow">switch</span> (baudrate_raw) {</div><div class="line">      <span class="keywordflow">case</span> 1000: baudrate = <a class="code" href="canlib_8h.html#ac823cae7d94763607a7ca40fcdb5196d">canBITRATE_1M</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 500:  baudrate = <a class="code" href="canlib_8h.html#ae72ee3c12379c4817de3b53c7cc6d89c">canBITRATE_500K</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 250:  baudrate = <a class="code" href="canlib_8h.html#a87f618db13ec4f48610def72cfb40969">canBITRATE_250K</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 125:  baudrate = <a class="code" href="canlib_8h.html#a29a9aafde6e861b4fffb72ad124aeed1">canBITRATE_125K</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">default</span>:   usage ();</div><div class="line">                 <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (!send_trig &amp;&amp; !send_digital &amp;&amp; !send_ramp) usage ();</div><div class="line"></div><div class="line">  freq = baudrate;</div><div class="line">  <a class="code" href="group___c_a_n.html#gaf38b95fce4930347d9986887ec046e13">canTranslateBaud</a> (&amp;freq, &amp;tseg1, &amp;tseg2, &amp;sjw, &amp;nosamp, &amp;sync_mode);</div><div class="line">  prescaler = 16000000L / freq / (tseg1 + tseg2 + 1) / 2;</div><div class="line">  btr0 = 0;</div><div class="line">  btr1 = 0;</div><div class="line">  btr0 |= ((sjw - 1) &amp; 0x03) &lt;&lt; 6;</div><div class="line">  btr0 |= (prescaler - 1) &amp; 0x3f;</div><div class="line">  btr1 |= (nosamp == 3) ? 0x80 : 0;</div><div class="line">  btr1 |= ((tseg2 - 1) &amp; 0x07) &lt;&lt; 4;</div><div class="line">  btr1 |= (tseg1 - 1) &amp; 0x0f;</div><div class="line"></div><div class="line">  printf (<span class="stringliteral">&quot;Parameters: \n&quot;</span>);</div><div class="line">  printf (<span class="stringliteral">&quot;  debug_level=%d\n&quot;</span>, debug_level);</div><div class="line">  printf (<span class="stringliteral">&quot;  baudrate=%d\n&quot;</span>, baudrate);</div><div class="line">  printf (<span class="stringliteral">&quot;  baudrate_raw=%d/0x%08x\n&quot;</span>, baudrate_raw, baudrate_raw);</div><div class="line">  printf (<span class="stringliteral">&quot;  freq=%ld\n&quot;</span>, freq);</div><div class="line">  printf (<span class="stringliteral">&quot;  prescaler=%d\n&quot;</span>, prescaler);</div><div class="line">  printf (<span class="stringliteral">&quot;  channel=%d\n&quot;</span>, channel);</div><div class="line">  printf (<span class="stringliteral">&quot;  btr0=%u/0x%02x, btr1=%u/0x%02x\n&quot;</span>, btr0, btr0, btr1, btr1);</div><div class="line">  printf (<span class="stringliteral">&quot;  tseg1=%d, tseg2=%d\n&quot;</span>, tseg1, tseg2);</div><div class="line"></div><div class="line"></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a> (channel, 0);</div><div class="line">  errorExit (<span class="stringliteral">&quot;canOpenChannel()&quot;</span>, (<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>) hnd);</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> ((btr0 != 0) &amp;&amp; (btr1 != 0)) {</div><div class="line">    stat = <a class="code" href="group___c_a_n.html#gac34c1577cae55385250e7412513c8cbd">canSetBusParamsC200</a> (hnd, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>) btr0, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>) btr1);</div><div class="line">  }</div><div class="line">  <span class="keywordflow">else</span> {</div><div class="line">    stat = <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a> (hnd, <a class="code" href="canlib_8h.html#ac823cae7d94763607a7ca40fcdb5196d">canBITRATE_1M</a>, 0, 0, 0, 0, 0);</div><div class="line">  }</div><div class="line"></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga99c7c99cc71580f8099a1407f4f9ea1a">canBusOn</a> (hnd);</div><div class="line">  errorExit (<span class="stringliteral">&quot;canBusOn&quot;</span>, stat);</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaeaa24db97af22478ca51d48636c7bb12">canIoCtl</a> (hnd,</div><div class="line">            canIOCTL_GET_EVENTHANDLE,</div><div class="line">            &amp;notification_event,</div><div class="line">            <span class="keyword">sizeof</span> (notification_event));</div><div class="line"></div><div class="line">  timeBeginPeriod (1);</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (rt) {</div><div class="line">    <a class="code" href="kvmlib_8h.html#aa8c0374618b33785ccb02f74bcfebc46">HANDLE</a> thread_handle;</div><div class="line">    <a class="code" href="kvmlib_8h.html#aa8c0374618b33785ccb02f74bcfebc46">HANDLE</a> process_handle;</div><div class="line"></div><div class="line">    process_handle = GetCurrentProcess ();</div><div class="line">    SetPriorityClass (process_handle, REALTIME_PRIORITY_CLASS);</div><div class="line">    thread_handle = GetCurrentThread ();</div><div class="line">    SetThreadPriority (thread_handle, THREAD_PRIORITY_TIME_CRITICAL);</div><div class="line">  }</div><div class="line"></div><div class="line">  tstart = get_time_1ms ();</div><div class="line"></div><div class="line">  iterations = 0;</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (!ready) {</div><div class="line">    <span class="keywordtype">int</span> active_handle = 0;</div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> msg [8];</div><div class="line">    <span class="keywordtype">int</span> flags, dlc;</div><div class="line"></div><div class="line">    <span class="keywordtype">double</span> ts = ((double) get_time_1ms () - tstart) / 1000;</div><div class="line"></div><div class="line">    ++iterations;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (send_trig) {</div><div class="line">      <span class="keywordtype">int</span> cos_active = (iterations % 2) != 0;</div><div class="line"></div><div class="line">      <span class="keywordtype">double</span> sin1 = sin (ts / sin1_period * M_PI * 2) * sin1_amp,</div><div class="line">             sin2 = sin (ts / sin2_period * M_PI * 2) * sin2_amp + sin2_amp,</div><div class="line">             cos1 = cos (ts / cos1_period * M_PI * 2) * cos1_amp,</div><div class="line">             cos2 = cos (ts / cos2_period * M_PI * 2) * cos2_amp + cos2_amp;</div><div class="line"></div><div class="line">      <span class="keywordtype">int</span> sin_state = 0;</div><div class="line">      <span class="keywordflow">if</span> (sin1 &gt; 0) sin_state = 2;</div><div class="line">      <span class="keywordflow">if</span> (sin1 &lt; 0) sin_state = 1;</div><div class="line"></div><div class="line">      clear_msg (msg);</div><div class="line">      store_int (msg,  0,  2, sin_state, motorola);</div><div class="line">      store_int (msg, 16, 16, (<span class="keywordtype">int</span>) sin1, motorola);</div><div class="line">      store_int (msg, 32, 16, (<span class="keywordtype">int</span>) cos1, motorola);</div><div class="line">      store_int (msg, 48,  8, cos_active, motorola);</div><div class="line">      <span class="keywordflow">if</span> (cos_active) store_int (msg, 56,  8, (<span class="keywordtype">int</span>) cos2, motorola);</div><div class="line">                 <span class="keywordflow">else</span> store_int (msg, 56,  8, (<span class="keywordtype">int</span>) sin2, motorola);</div><div class="line"></div><div class="line">      dlc = 8;</div><div class="line">      flags = <a class="code" href="canstat_8h.html#a56000a3f22f1408c26db4dc731aa4a9e">canMSG_STD</a>;</div><div class="line">      stat = <a class="code" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite</a> (hnd, trig_id, msg, dlc, flags);</div><div class="line">      <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        seq++;</div><div class="line">        <span class="keywordflow">if</span> ((seq % 1000) == 0) printf (<span class="stringliteral">&quot;*&quot;</span>);</div><div class="line">      }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (send_digital) {</div><div class="line">      <span class="keywordtype">int</span> d = 512;</div><div class="line">      <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> t1 = ((iterations / d      ) &amp; 0x01) * 255,</div><div class="line">                   t2 = ((iterations / d /   2) &amp; 0x01) * 255,</div><div class="line">                   t3 = ((iterations / d /   4) &amp; 0x01) * 255,</div><div class="line">                   t4 = ((iterations / d /   8) &amp; 0x01) * 255,</div><div class="line">                   t5 = ((iterations / d /  16) &amp; 0x01) * 255,</div><div class="line">                   t6 = ((iterations / d /  32) &amp; 0x01) * 255,</div><div class="line">                   t7 = ((iterations / d /  64) &amp; 0x01) * 255,</div><div class="line">                   t8 = ((iterations / d / 128) &amp; 0x01) * 255;</div><div class="line">      clear_msg (msg);</div><div class="line">      store_int (msg,  0,  8, t1, motorola);</div><div class="line">      store_int (msg,  8,  8, t2, motorola);</div><div class="line">      store_int (msg, 16,  8, t3, motorola);</div><div class="line">      store_int (msg, 24,  8, t4, motorola);</div><div class="line">      store_int (msg, 32,  8, t5, motorola);</div><div class="line">      store_int (msg, 40,  8, t6, motorola);</div><div class="line">      store_int (msg, 48,  8, t7, motorola);</div><div class="line">      store_int (msg, 56,  8, t8, motorola);</div><div class="line"></div><div class="line">      dlc = 8;</div><div class="line">      flags = <a class="code" href="canstat_8h.html#a56000a3f22f1408c26db4dc731aa4a9e">canMSG_STD</a>;</div><div class="line">      stat = <a class="code" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite</a> (hnd, digital_id, msg, dlc, flags);</div><div class="line">      <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        seq++;</div><div class="line">        <span class="keywordflow">if</span> ((seq % 1000) == 0) printf (<span class="stringliteral">&quot;#&quot;</span>);</div><div class="line">      }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (send_ramp) {</div><div class="line">      <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> tri = iterations % 4000,</div><div class="line">                   ramp = iterations % 1000;</div><div class="line">      <span class="keywordflow">if</span> (tri &gt;= 2000) tri = 4000 - tri;</div><div class="line">      tri -= 1000;</div><div class="line">      clear_msg (msg);</div><div class="line">      store_int (msg,  0, 16, ramp, motorola);</div><div class="line">      store_int (msg, 16, 16, tri, motorola);</div><div class="line"></div><div class="line">      dlc = 8;</div><div class="line">      flags = <a class="code" href="canstat_8h.html#a56000a3f22f1408c26db4dc731aa4a9e">canMSG_STD</a>;</div><div class="line">      stat = <a class="code" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite</a> (hnd, ramp_id, msg, dlc, flags);</div><div class="line">      <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        seq++;</div><div class="line">        <span class="keywordflow">if</span> ((seq % 1000) == 0) printf (<span class="stringliteral">&quot;+&quot;</span>);</div><div class="line">      }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (send_float) {</div><div class="line">      <span class="keywordtype">double</span> sin1 = sin (ts / sin1_period * M_PI * 2) * sin1_amp *  10,</div><div class="line">             cos1 = cos (ts / cos1_period * M_PI * 2) * cos1_amp * 100;</div><div class="line"></div><div class="line">      clear_msg (msg);</div><div class="line">      store_float (msg,  0, (<span class="keywordtype">float</span>) sin1, motorola);</div><div class="line">      store_float (msg, 32, (<span class="keywordtype">float</span>) cos1, motorola);</div><div class="line"></div><div class="line">      dlc = 8;</div><div class="line">      flags = <a class="code" href="canstat_8h.html#a56000a3f22f1408c26db4dc731aa4a9e">canMSG_STD</a>;</div><div class="line">      stat = <a class="code" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite</a> (hnd, float_id, msg, dlc, flags);</div><div class="line">      <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        seq++;</div><div class="line">        <span class="keywordflow">if</span> ((seq % 1000) == 0) printf (<span class="stringliteral">&quot;.&quot;</span>);</div><div class="line">      }</div><div class="line">    }</div><div class="line"></div><div class="line"></div><div class="line">    active_handle = 0;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7ae895fc4d04df717a1a79c144bd2f358e">canERR_TXBUFOFL</a>) printf (<span class="stringliteral">&quot;Error: TX overflow\n&quot;</span>);</div><div class="line"></div><div class="line">    { <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> n;</div><div class="line">      INPUT_RECORD ir;</div><div class="line">      <span class="keywordflow">if</span> (PeekConsoleInput (GetStdHandle (STD_INPUT_HANDLE), &amp;ir, 1, &amp;n)) {</div><div class="line">        <span class="keywordflow">if</span> (n != 0) active_handle = 1;</div><div class="line">      }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (active_handle == 1) { <span class="comment">/* keyboard */</span></div><div class="line">      <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> n;</div><div class="line">      INPUT_RECORD ir;</div><div class="line"></div><div class="line">      ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &amp;ir, 1, &amp;n);</div><div class="line">      <span class="keywordflow">if</span> ((n == 1) &amp;&amp; (ir.EventType == KEY_EVENT)) {</div><div class="line">        <span class="keywordflow">if</span> (ir.Event.KeyEvent.bKeyDown) {</div><div class="line">          <span class="keywordflow">switch</span> (ir.Event.KeyEvent.uChar.AsciiChar) {</div><div class="line"></div><div class="line">            <span class="keywordflow">case</span> <span class="charliteral">&#39;q&#39;</span>: <span class="comment">/* change debug level */</span></div><div class="line">                 <span class="keywordflow">if</span> (debug_level &gt; 0) debug_level = 0;</div><div class="line">                                 <span class="keywordflow">else</span> debug_level = 1;</div><div class="line">                 printf (<span class="stringliteral">&quot;debug_level=%d\n&quot;</span>, debug_level);</div><div class="line">                 <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">            <span class="keywordflow">case</span> <span class="charliteral">&#39;h&#39;</span>: <span class="comment">/* print help */</span></div><div class="line">                 help ();</div><div class="line">                 <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line"></div><div class="line">            <span class="keywordflow">case</span> 27: <span class="comment">/* ESC: quit */</span></div><div class="line">                 ready = TRUE;</div><div class="line">                 <span class="keywordflow">break</span>;</div><div class="line">          }</div><div class="line">        }</div><div class="line">      }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (extra_sleep) Sleep(extra_sleep + 1);</div><div class="line">    <span class="keywordflow">else</span> Sleep (1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="group___c_a_n.html#gaf1786cfbfd542b18b9c599d278837bd9">canBusOff</a> (hnd);</div><div class="line">  <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a> (hnd);</div><div class="line"></div><div class="line">  timeEndPeriod (1);</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">} <span class="comment">/* main */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">/***************************************************************************/</span></div><div class="line"></div><div class="line"></div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_kvadblib.html">Database API (kvaDbLib)</a></li><li class="navelem"><a class="el" href="page_user_guide_kvadblib_samples.html">Sample Programs (kvaDBLib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
